$("#mine").load(function () {
    getUserInfo();
})
$('#recharge').load(function () {
    $('.recharge-detail-wrap').empty();
    getWalletLIst();
})
$('#address').load(function () {
    $('.wrap-all-address').empty();
    getAddressList();
    $('.address-eidt').click(function (e) {
        console.info(e)
    })
})
$('#rechargeTo').load(function () {
    $('.recharge-balance').text(RssUser.Data.balance);
    $('.recharge-button').click(function () {
        if(RssUser.Data.balance < 0) {
            alert('不足1元，无法提现');
            return;
        }
        new Ajax('withdraw/append').keymyid().keyvalue({channelid: 1, fee: 20}).getJson(function (data) {
            if(data.rsscode === 100) {
                alert('提现成功');
                window.location.href='index.html'
            } else {
                alert('提现失败');
            }
            console.info(data,'data');
        })
    })
})
$('#addAddress').load(function () {
    if ($('input[name=addid]').val().length > 0) {
        const name = $('input[name=addid1]').val();
        const tel = $('input[name=addid2]').val();
        const addr = $('input[name=addid3]').val();
        $('input[name=name]').val(name);
        $('input[name=telphone]').val(tel);
        $('input[name=addr]').val(addr);
    }
    $('.add-address-btn').click(function () {
        // var dict = new RssDict($('#add-address-form').serializeArray()).keymyid().getDict();
        // ValidatedV4(dict).isMobphone('telphone', '手机号格式错误')
        if ($('input[name=addid]').val().length === 0) {
        new Ajax('user/addr/edit').keymyid().keyvalue($('#add-address-form').serializeArray()).getJson(function (data) {
            if (!data.rsscode) {
                alert('保存成功');
                window.location.href = 'index.html'
            } else {
                alert('保存失败');
            }
            console.info(data, 'data');
        })
    } else {
            new Ajax('user/addr/edit').condition({id: $('input[name=addid]').val(), myid: RssUser.Data.myid}).keyvalue($('#add-address-form').serializeArray()).getJson(function (data) {
                if(!data.rsscode) {
                    alert('保存成功');
                    window.location.href='index.html'
                } else {
                    alert('保存失败');
                }
                console.info(data,'data');
            })
        }

    })
})
function saveUserInfo() {
    new Ajax("user/update").keyvalue("nickname", $('.mine-nickname').val()).keyvalue("description", $('.mine-description').val()).condition({myid: RssUser.Data.myid}).getJson(function (data) {
        getUserInfo();
    })
}
function getUserInfo() {
    new Ajax('user/detail').condition({myid: RssUser.Data.myid}).getJson(function (data) {
        $('.mine-nickname').val(data.nickname);
        $('.mine-description').val(data.description.length === 0?'暂未添加描述':data.description);
    })
}
function toWallet() {
    window.location.hash="#recharge"
}
function getWalletLIst() {
    console.info( RssUser.Data)
    $('.wallet-balance').text(RssUser.Data.balance)
    new Ajax('withdraw/list').condition({myid: RssUser.Data.myid}).getJson(function (data) {
        console.info(data);
        let str = '';
        for (let i = 0; i < data.length; i ++) {
            str += '<div class="recharge-div">\n' +
                '                    <div class="recharge-div-flex">用户'+data[i].nickname+'提现'+data[i].fee+'元</div>\n' +
                '                    <div class="recharge-div-time">'+formatTest1(data[i].shijian)+'</div>\n' +
                '                    <div>'+formatTest2(data[i].shijian)+'</div>\n' +
                '                </div>'
        }
        $('.recharge-detail-wrap').append(str);
    })
}
function toAddress() {
    window.location.hash = '#address'
}
function toAddAddress() {
    $('input[name=addid]').val('');
    window.location.hash = '#addAddress'
}
function getAddressDetail(id) {
    new Ajax('user/addr/detail').condition({id: id, myid: RssUser.Data.myid}).getJson(function (data) {
        console.info(data,'data');
    })
}
function getAddressList() {
    new Ajax('user/addr/list').condition({myid: RssUser.Data.myid}).getJson(function (data) {
        console.info(data);
        let str = '';
        for(let i = 0; i < data.length; i++) {
            str += '<div class="wrap-address">\n' +
                '                <div class="wrap-address-flex">\n' +
                '                    <div class="address-name">'+data[i].name+'</div>\n' +
                '                    <div>'+data[i].telphone+'</div>\n' +
                '                </div>\n' +
                '                <div class="address-detail">'+data[i].addr+'</div>\n' +
                '                <div class="wrap-address-flex">\n' +
                '                    <div class="address-wrap-img"><img src="./img/tmp/default.png" class="address-default">默认地址</div>\n' +
                '                    <div class="address-eidt" onclick="editAddress('+data[i].id+', '+data[i].name+', '+data[i].telphone+', '+data[i].addr+')">编辑</div>\n' +
                '                    <div class="address-delete" onclick="deleteAddr('+data[i].id+')">删除</div>\n' +
                '                </div>\n' +
                '            </div>'
        }
        $('.wrap-all-address').append(str)
    })
}
function editAddress(id, name, tel ,addr) {
    window.location.hash = '#addAddress';
    $('input[name=addid]').val(id);
    $('input[name=addid1]').val(name);
    $('input[name=addid2]').val(tel);
    $('input[name=addid3]').val(addr);
}
function deleteAddr(id) {
    new Ajax('user/addr/delete').condition({id: id, myid: RssUser.Data.myid}).keyvalue($('#add-address-form').serializeArray()).getJson(function (data) {
        if(!data.rsscode) {
            alert('删除成功');
            window.location.href='index.html'
        } else {
            alert('删除失败');
        }
        console.info(data,'data');
    })
}
function rechargeTo() {
    window.location.hash = "#rechargeTo"
}
